﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;

namespace WpfWaltz
{
   /// <summary>
   /// Interaction logic for MainWindow.xaml
   /// </summary>
   public partial class MainWindow : Window
   {
      public List<SampleApplication> SampleList { get; set; }
      
      public MainWindow()
      {
         InitializeComponent();
         SampleList = PopulateSampleList();
         DataContext = this;
      }

      private List<SampleApplication> PopulateSampleList()
      {
         List<SampleApplication> sampleList = new List<SampleApplication>();

         sampleList.Add(
            new SampleApplication()
            {
               Description = "WPF String Formatting",
               URL = "2012/03/wpf-string-formatting.html",
               WindowType = typeof(StringFormatting.MainWindow)
            }
            );

         sampleList.Add(
            new SampleApplication()
            {
               Description = "DataGrid loading message User Control",
               URL = "2012/02/datagrid-loading-message-user-control.html",
               WindowType = typeof(DataGridWatermark.MainWindow)
            }
            );
         
         sampleList.Add(
            new SampleApplication()
            {
               Description = "WPF ComboBox Data Binding",
               URL = "2008/10/binding-enum-property-blockbookingdata.html",
               WindowType = typeof(ComboBoxDataBinding.MainWindow)
            }
            );

         sampleList.Add(
            new SampleApplication()
            {
               Description = "DataTemplateSelector",
               URL = "2012/03/datatype-instead-of-datatemplateselecto.html",
               WindowType = typeof(DataTemplateSelectorNS.MainWindow)
            }
            );

         sampleList.Add(
            new SampleApplication()
            {
               Description = "Simple ColorPicker",
               URL = "2012/03/simple-wpf-colorpicker-combobox.html",
               WindowType = typeof(SimpleColorPicker.MainWindow)
            }
            );
         
         return sampleList;
      }

      private void ShowSampleWindow(object sender, RoutedEventArgs e)
      {
         // Cast the e.Source to a Button so we can access it's CommandParameter
         Button b = (Button)(e.Source);

         // The Button's CommandParameter is bound to the WindowType property
         Type t = (Type)b.CommandParameter;

         // Create an instance of the supplied type
         object sampleWindow = Activator.CreateInstance(t);

         // Sanity check to make sure the newly minted
         // object is a Window that we can Show
         if (sampleWindow is Window)
         {
            ((Window)sampleWindow).Show();
         }
      }

      private void Hyperlink_Click(object sender, RoutedEventArgs e)
      {
         Process.Start((sender as Hyperlink).NavigateUri.ToString());
      }
   }
}
